package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConCalendar;
import com.htgd.gardenexpo.entity.GeConVenue;
import com.htgd.gardenexpo.entity.PageQuery;
import com.htgd.gardenexpo.page.VenueQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.GeConVenueService;
import com.htgd.gardenexpo.utils.SecurityParameter;
import com.htgd.gardenexpo.utils.UniteUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 新闻表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-11
 */
@RestController
@RequestMapping("/api/venue")
@Api(tags = "场馆预约")
@Slf4j
public class GeConVenueController {

    @Autowired
    private GeConVenueService geConVenueService;

    @Autowired
    private GeBaseDictService geBaseDictService;

    /**
     * 新增展馆
     */
    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add(@RequestBody GeConVenue geConVenue) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
//            String uploadIds = geConVenue.getUploadIds();
            geConVenueService.insert(geConVenue);
//            if (!uploadIds.equals("")){
//                for (String uploadId:uploadIds.split(",")){
//                    GeConNewsUpload geConNewsUpload=geConNewsUploadService.queryById(Integer.parseInt(uploadId));
//                    geConNewsUpload.setNewId(geConCalendar.getNewId());
//                    geConNewsUploadService.update(geConNewsUpload);
//                }
//            }
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }

        return result;
    }

    /**
     * 编辑展馆
     */
    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit(@RequestBody GeConVenue geConVenue) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            String uploadIds = geConVenue.getUploadIds();
            geConVenueService.updateEntity(geConVenue);
//            if (!uploadIds.equals("")&&uploadIds.contains(",")){
//                geConNewsUploadService.deleteByIds(uploadIds.split(","),geConCalendar.getNewId());
//                for (String uploadId:uploadIds.split(",")){
//                    GeConNewsUpload geConNewsUpload=geConNewsUploadService.queryById(Integer.parseInt(uploadId));
//                    geConNewsUpload.setNewId(geConCalendar.getNewId());
//                    geConNewsUploadService.update(geConNewsUpload);
//                }
//            }else {
//                geConNewsUploadService.deleteByNewId(geConCalendar.getNewId());
//            }
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     * 删除展馆
     */
    @PostMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult<JSONObject> delete(int newId) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConVenueService.deleteById(newId);
//            List<GeConNewsUpload> list=geConNewsUploadService.queryByNewsId(newId);
//            for (GeConNewsUpload geConNewsUpload:list){
//                String filepath=geConNewsUpload.getFilePath();
//                String path=filepath.substring(filepath.indexOf(bucket_name+"/"),filepath.length()-1);
//                getAmazonS3Client.delete(path);
//            }
//            geConNewsUploadService.deleteByNewId(newId);
//            geConNewsFeedbackService.deleteById(newId);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     * 展馆列表
     */
    @PostMapping(value = "/list")
    @ApiOperation("查询")
    @SecurityParameter
    public CommonResult<List<GeConVenue>> list(@RequestBody VenueQuery venueQuery) {
        CommonResult<List<GeConVenue>> result = new CommonResult<>();
        try {
            venueQuery.setPageNum((venueQuery.getPageNum() - 1) * venueQuery.getPageSize());
            List<GeConVenue> list = geConVenueService.getAllNewsByValue(venueQuery);
            if (list != null && list.size() > 1) {
                for (GeConVenue geConVenue : list) {
                    String content = UniteUtils.stripHtml(geConVenue.getNewContent().toString());
                    if (!content.equals("")) {
                        geConVenue.setNewContent(content.substring(0, content.length() > 200 ? 200 : content.length()));
                    } else {
                        geConVenue.setNewContent("");
                    }
                    geConVenue.setRecommendTypeText(geBaseDictService.getRecommendTypeText(geConVenue.getRecommendType()));
                    if(Objects.equals(geConVenue.getHot(),"0")) geConVenue.setHotText("普通");
                    if(Objects.equals(geConVenue.getHot(),"1")) geConVenue.setHotText("热门");
                }
            }
            venueQuery.setPageSize(0);
            List<GeConVenue> list1 = geConVenueService.getAllNewsByValueNoContent(venueQuery);
            result.setCode(200);
            result.setData(list);
            result.setMessage(String.valueOf(list1.size()));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     * 展馆审核
     */
    @GetMapping(value = "/review")
    @ApiOperation("审批")
    public CommonResult<JSONObject> review( GeConVenue geConVenue){
        CommonResult<JSONObject> result = new CommonResult<>();
        try{
            GeConVenue geConNews1=geConVenueService.queryById(geConVenue.getNewId());
            geConNews1.setStatus(geConVenue.getStatus());
            if (geConVenue.getPublisherId()!=null){
                geConNews1.setPublisherId(geConVenue.getPublisherId());
            }
            if (geConVenue.getAuthUserId()!=null){
                geConNews1.setAuthUserId(geConVenue.getAuthUserId());
            }
//            String feedback=geConVenue.getFeedback();
            geConNews1.setReleasetime(new Date());
            geConVenueService.updateEntity(geConNews1);
//            if (!feedback.equals("")){
//                GeConNewsFeedback geConNewsFeedback=new GeConNewsFeedback();
//                geConNewsFeedback.setNewId(geConCalendar.getNewId());
//                geConNewsFeedback.setContent(feedback);
//                geConNewsFeedbackService.insert(geConNewsFeedback);
//            }
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     *  展馆详情
     */
    @PostMapping(value = "/details")
    @ApiOperation("详情")
    @SecurityParameter
    public CommonResult<Map<String,Object>> details(@RequestBody String s){
        CommonResult<Map<String, Object>> result = new CommonResult<>();
        try{
            Integer newId= JSON.parseObject(s).getInteger("newId");
            Map<String, Object> map = new HashMap<>();
            GeConVenue geConVenue=geConVenueService.queryById(newId);
            geConVenue.setRecommendTypeText(geBaseDictService.getRecommendTypeText(geConVenue.getRecommendType()));
            if(Objects.equals(geConVenue.getHot(),"0")) geConVenue.setHotText("普通");
            if(Objects.equals(geConVenue.getHot(),"1")) geConVenue.setHotText("热门");
            map.put("news",geConVenue);
//            List<GeConNewsUpload> geConNewsUpload=geConNewsUploadService.queryByNewsId(newId);
//            map.put("upload",geConNewsUpload);
            VenueQuery venueQuery = new VenueQuery();
            venueQuery.setReviewStatus("3");
            venueQuery.setPageSize(0);
            List<GeConVenue> nlist=geConVenueService.getAllNewsByValueNoContent(venueQuery);
            List<GeConVenue> list = new ArrayList<>();
            for (int i=0;i<nlist.size();i++){
                GeConVenue geConNews1=nlist.get(i);
                if (newId==geConNews1.getNewId()){
                    if (nlist.size() >= 3) {
                        if (i < nlist.size() - 3) {
                            if (i != 0) {
                                map.put("up", Conversion(nlist.get(i - 1)));
                            } else {
                                map.put("up", "");
                            }
                            map.put("down", Conversion(nlist.get(i + 1)));
                            list.add(Conversion(nlist.get(i + 2)));
                            list.add(Conversion(nlist.get(i + 3)));
                            map.put("more", list);
                        } else if (i == nlist.size() - 3) {
                            if (i != 0) {
                                map.put("up",Conversion(nlist.get(i - 1)));
                            } else {
                                map.put("up", "");
                            }
                            map.put("down", Conversion(nlist.get(i + 1)));
                            list.add(Conversion(nlist.get(i + 2)));
                            if (nlist.size() != 3) {
                                list.add(Conversion(nlist.get(0)));
                            }
                            map.put("more", list);
                        } else if (i == nlist.size() - 2) {
                            if (i != 0) {
                                map.put("up", Conversion(nlist.get(i - 1)));
                            } else {
                                map.put("up", "");
                            }
                            map.put("down", Conversion(nlist.get(i + 1)));
                            if (nlist.size() != 3) {
                                list.add(Conversion(nlist.get(0)));
                                list.add(Conversion(nlist.get(1)));
                            }
                            map.put("more", list);
                        } else if (i == nlist.size() - 1) {
                            if (i != 0) {
                                map.put("up",Conversion(nlist.get(i - 1)));
                            } else {
                                map.put("up", "");
                            }
                            map.put("down", "");
                            list.add(Conversion(nlist.get(0)));
                            if (nlist.size() != 3) {
                                list.add(Conversion(nlist.get(1)));
                            }
                            map.put("more", list);
                        }
                    } else {
                        if (i == 0 && nlist.size() > 1) {
                            map.put("up", "");
                            map.put("down", Conversion(nlist.get(i + 1)));
                        } else if (i == 0 && nlist.size() == 1) {
                            map.put("up", "");
                            map.put("down", "");
                        } else {
                            map.put("up", Conversion(nlist.get(i - 1)));
                            map.put("down", "");
                        }
                        map.put("more", "");
                    }
                    break;
                }
            }
            result.setData(map);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/getVenueNums")
    @ApiOperation("用于获取各个场馆预约人数")
    public CommonResult getVenueNums(String date){
        log.info("综管-用于获取各个场馆预约人数：{}",date);
       return geConVenueService.getVenueNums(date);
    }

    public GeConVenue  Conversion(GeConVenue geConVenue){
        GeConVenue geConNews1=geConVenueService.queryById(geConVenue.getNewId());
        String content=UniteUtils.stripHtml(geConNews1.getNewContent().toString());
        if (!content.equals("")){
            geConNews1.setNewContent(content.substring(0,content.length()>200?200:content.length()));
        }else {
            geConNews1.setNewContent("");
        }
        return geConNews1;
    }
}
